//
//  GZStatusCell.swift
//  sinaweibo
//
//  Created by mac book pro on 16/3/26.
//  Copyright © 2016年 finish. All rights reserved.
//

import UIKit
import SnapKit
/// 控件之间间距
let CZStatusCellMargin: CGFloat = 8
/// 自定义cell
class GZStatusCell: UITableViewCell {
    /// bottomView上约束
    var bottomViewTopCons: Constraint?
    var status: GZStatus?{
        didSet{
            originalView.status = status
            
            self.bottomViewTopCons?.uninstall()
            if let retweetStatus = status?.retweeted_status{
                retweetView.hidden = false
                retweetView.status = retweetStatus
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else{
                retweetView.hidden = true
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 设置背景颜色
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        prepareUI()
    }
    
    private func prepareUI(){
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        //添加约束
        //originalView约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(CZStatusCellMargin)
            make.trailing.equalTo(contentView)
        }
        //retweetView约束
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        //底部view约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
            self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        //contentview约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            //底部约束根据内容来
            make.bottom.equalTo(bottomView)
        }
        
    }
    //MARK: -lazy load
    /// 原来视图
    private lazy var originalView: GZStatusOriginalView = GZStatusOriginalView()
    /// 转发微博
    private lazy var retweetView: GZStatusRetweetView = GZStatusRetweetView()
    /// 底部视图
    private lazy var bottomView: GZStatusButtonView = GZStatusButtonView()
}
